import json
from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from rest_framework.authentication import TokenAuthentication
from rest_framework.response import Response
from rest_framework.views import APIView
from apitest3.models import IpVoiceInfo, stuTest, toDo
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination,CursorPagination

class IpVoiceSerializer(serializers.ModelSerializer):
    status = serializers.CharField(source='get_status_display',read_only=True)
    class Meta:
        model = IpVoiceInfo
        fields = "__all__"
        depth = 1

class stuTestSerializer(serializers.ModelSerializer):
    class Meta:
        model = stuTest
        fields = "__all__"

class IpVoiceView(APIView):
    authentication_classes = []
    def get(self,request,*args,**kwargs):
        info = IpVoiceInfo.objects.filter(is_active='true')
        pg = MyCursorPagination()
        pginfo = pg.paginate_queryset(queryset=info, request=request, view=self)
        ser = IpVoiceSerializer(instance=pginfo, many=True)
        return pg.get_paginated_response(ser.data)

class MyLimitOffsetPagination(LimitOffsetPagination):
    default_limit = 10
    limit_query_param = 'limit'
    offset_query_param = 'offset'
    max_limit = 50

class MyCursorPagination(CursorPagination):

    cursor_query_param = 'cursor'
    page_size = 10
    ordering = 'id'
    page_size_query_param = None
    max_page_size = None
    offset_cutoff = 1000

from rest_framework.viewsets import ModelViewSet


class LargeResultsSetPagination(PageNumberPagination):
    # 默认每页显示10条数据
    page_size = 10
    # 根据'page_size'关键字自定义每页显示多少条数据
    page_size_query_param = 'page_size'
    # 自定义'pag'关键字查询具体某一页的数据（默认为'page'）
    # 如：?pag=3 即查询第三页的数据
    page_query_param = 'pag'
    # 每页最多显示15条数据
    # 克制 ?page_size=20,这样自定义无效，每页最多显示15条数据
    max_page_size = 20

class PhoneView(ModelViewSet):
    authentication_classes = []
    queryset = IpVoiceInfo.objects.all()
    serializer_class = IpVoiceSerializer
    pagination_class = LargeResultsSetPagination


class StuView(ModelViewSet):
    queryset = stuTest.objects.all()
    serializer_class = stuTestSerializer
    # pagination_class = LargeResultsSetPagination

class toDoSerializer(serializers.ModelSerializer):
    class Meta:
        model = toDo
        fields = "__all__"

class toDoView(ModelViewSet):
    queryset = toDo.objects.all()
    serializer_class = toDoSerializer





